﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Dotnet.Code.Extend
{
    public static partial class Ext
    {
        public static string UrlEncode(this string str,System.Text.Encoding encoding)
        {
            string encode = HttpUtility.UrlEncode(str, encoding);
            return encode.Replace("+", "%20");
        }
        /// <summary>
        /// 默认编码为UTF8模式
        /// </summary>
        /// <param name="str">待编码字符串</param>
        /// <returns>编码字符串</returns>
        public static string UrlEncode(this string str)
        {
            return UrlEncode(str, Encoding.UTF8);
        }
        public static bool IsNumeric(this string s)
        {
            s = s.Trim(' ');
            if (string.IsNullOrEmpty(s)) return false;
            //if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

        public static string md5(this string s)
        {
            //实例化一个md5对像
            MD5 md5 = MD5.Create();
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
            StringBuilder sb = new StringBuilder();
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < bytes.Length; i++)
            {
                //加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
                sb.Append(bytes[i].ToString("x2"));
            }
            return sb.ToString().Trim();
        }

        public static bool isUnixTime(this string s)
        {
            s = s.Trim(' ');
            if (string.IsNullOrEmpty(s))
                return false;
            int len = s.Length;
            if (len>13)
                return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }
        public static DateTime UnixTimeToDateTime(this string str)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            int result = 0;
            string sub = "";
            if (str.Length > 10)
            {
                str = str.Substring(0, 10);
            }
            bool ret = Int32.TryParse(str, out result);
            if (ret == true)
            {
                dtDateTime = dtDateTime.AddSeconds(result);
            }
            return dtDateTime.ToLocalTime();
        }
        public static void PlayWav(this string txt,string dir="")
        {
            try
            {
                string mp3path = System.IO.Directory.GetCurrentDirectory()+"\\";
                if (string.IsNullOrEmpty(dir) == false)
                {
                    mp3path = mp3path + dir+"\\";
                }
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                player.SoundLocation = mp3path + txt+".wav";
                player.Play();
                player.Dispose();
            }
            catch { }
        }
    }
}